package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    /**
     * 新增部门
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        deptService.add(dept);
        log.info("新增部门:{}", dept);
        return Result.success();
    }
    /**
     * 查询部门
     */
    @GetMapping
    public Result list(String  name,
                       Integer status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam (defaultValue = "10") Integer pageSize){
        log.info("查询部门,参数:name={},status={},page={},pageSize={}",name,status,page,pageSize);
       PageResult<Dept> pageResult = deptService.list(name,status,page,pageSize);
        return Result.success(pageResult);
    }
    /**
     * 根据id查询部门
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查询部门,参数:id={}",id);
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }
    /**
     * 修改部门
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门,参数:{}",dept);
        deptService.update(dept);
        return Result.success();
    }

    /**
     * 根据id删除部门
     */
    @Log
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer  id){
        log.info("删除部门,参数:{}",id);
        deptService.deleteById(id);
        return Result.success();
    }
    /**
     * 查询所有部门
     */
    @GetMapping("/list")
    public Result listAll(){

        List<Dept> depts =deptService.selectByStatus();
        return Result.success(depts);
    }
}
